#include "QT.h"
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMessageBox>

QT::QT(QWidget *parent)
    : QWidget(parent)
{   // 创建UI组件
   
    setupUI();

    // 连接信号和槽
    connectSignals();
   // ui.setupUi(this);

}

void QT::numberClicked()
{
    QPushButton* button = qobject_cast<QPushButton*>(sender());
    if (button) {
        QString number = button->text();
        display->setText(display->text() + number);
    }
}

void QT::operatorClicked()
{
    QPushButton* button = qobject_cast<QPushButton*>(sender());
    if (button) {
        QString op = button->text();
        if (op == "C") {
            // 清除
            display->clear();
            firstNumber = 0;
            currentOperator = "";
        }
        else if (op == "=") {
            // 计算结果
            calculateResult();
        }
        else {
            // 存储第一个数字和操作符
            firstNumber = display->text().toDouble();
            currentOperator = op;
            display->clear();
        }
    }
}

void QT::calculateResult()
{
    bool ok;
    double secondNumber = display->text().toDouble(&ok);

    if (!ok) {
        QMessageBox::warning(this, "错误", "无效的数字输入!");
        return;
    }

    double result = 0;

    // 根据操作符执行计算
    if (currentOperator == "+") {
        result = firstNumber + secondNumber;
    }
    else if (currentOperator == "-") {
        result = firstNumber - secondNumber;
    }
    else if (currentOperator == "*") {
        result = firstNumber * secondNumber;
    }
    else if (currentOperator == "/") {
        if (secondNumber == 0) {
            QMessageBox::warning(this, "错误", "除数不能为零!");
            return;
        }
        result = firstNumber / secondNumber;
    }
    else {
        return; // 没有操作符
    }

    display->setText(QString::number(result));
    currentOperator = "";
}

void QT::setupUI()
{
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 创建显示区域
    display = new QLineEdit();
    display->setReadOnly(true);
    display->setAlignment(Qt::AlignRight);
    display->setMaxLength(15);
    mainLayout->addWidget(display);

    // 创建按钮网格布局
    QGridLayout* buttonLayout = new QGridLayout();

    // 数字按钮
    QStringList numberButtons = {
        "7", "8", "9", "/",
        "4", "5", "6", "*",
        "1", "2", "3", "-",
        "0", "C", "=", "+"
    };

    int position = 0;
    for (int row = 0; row < 4; ++row) {
        for (int col = 0; col < 4; ++col) {
            QPushButton* button = new QPushButton(numberButtons[position]);
            button->setFixedSize(40, 40);
            buttonLayout->addWidget(button, row, col);

            // 根据按钮类型分类
            if (numberButtons[position] >= "0" && numberButtons[position] <= "9") {
                numberButtonList.append(button);
            }
            else {
                operatorButtonList.append(button);
            }

            position++;
        }
    }

    mainLayout->addLayout(buttonLayout);

    // 设置窗口属性
    setWindowTitle("简单计算器 - 学习C++与Qt");
    setFixedSize(400, 650);
}

void QT::connectSignals()
{
    // 连接数字按钮
    for (QPushButton* button : numberButtonList) {
        connect(button, &QPushButton::clicked, this, &QT::numberClicked);
    }

    // 连接操作符按钮
    for (QPushButton* button : operatorButtonList) {
        connect(button, &QPushButton::clicked, this, &QT::operatorClicked);
    }
}

QT::~QT()
{}

